//
//  SINATecApprovalManagerViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/2.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINATecApprovalManagerViewController.h"
#import "SINAUiviewClickLiserner.h"
#import "SINATecManagerSelectClassItemTableViewCell.h"
#import "SINATecManagerItemTableViewCell.h"
#import "SINATecManagerLeaveModel.h"
#import "SINATecApprovalManagerDetailViewController.h"
#import "SINACustomModelPickView.h"
#import "SINASubClassModel.h"

@interface SINATecApprovalManagerViewController ()<UITableViewDelegate,UITableViewDataSource,CustomModelPickViewDelegate>
//选择
@property (weak, nonatomic) IBOutlet UIView *selectClassView;
@property (weak, nonatomic) IBOutlet UIImageView *selectFlagImageView;
//筛选列表
@property (weak, nonatomic) IBOutlet UITableView *selectClassTableView;
@property (weak, nonatomic) IBOutlet UILabel *selectClassLabel;

@property(assign,nonatomic) NSInteger selectClassTableViewHeight;

@property (weak, nonatomic) IBOutlet UIView *selectContentView;
@property (weak, nonatomic) IBOutlet UITableView *contentTableView;

@property(nonatomic,strong) NSMutableArray *dataList;

@property(nonatomic,strong) NSMutableArray *classDataList;


@property(nonatomic,assign) BOOL  classSelectIsHidden;

@property(nonatomic,assign) NSInteger currentClassIndex;

@property(nonatomic,strong) SINASubClassModel *currentClassModel;

@end

@implementation SINATecApprovalManagerViewController
-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return  _dataList;
}
-(NSMutableArray *)classDataList{
    if (!_classDataList) {
        _classDataList = [NSMutableArray array];
    }
    return  _classDataList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"审批";
    
    self.classSelectIsHidden=YES;
    self.currentClassIndex=0;
    
    __weak __typeof(self) weakSelf = self;
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.selectClassView andWithBlick:^(UIView *view) {
        
        [weakSelf showSelectClassFromeBottomFunction];
        
    }];
    
    
    self.selectClassTableView.delegate=self;
    self.selectClassTableView.dataSource=self;
    self.selectClassTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.selectClassTableView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    self.selectClassTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.selectClassTableView.tag=100;
    self.selectClassTableView.showsVerticalScrollIndicator=NO;
    self.selectClassTableView.showsHorizontalScrollIndicator=NO;
    
    self.contentTableView.delegate=self;
    self.contentTableView.dataSource=self;
    self.contentTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.contentTableView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    self.contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.contentTableView.tag=101;
    self.contentTableView.showsVerticalScrollIndicator=NO;
    self.contentTableView.showsHorizontalScrollIndicator=NO;
    
    

    for (NSInteger i=0; i<0; i++) {
        
        SINATecManagerLeaveModel *model = [[SINATecManagerLeaveModel alloc]init];
        if (i%3==0) {
            model.status=1;
        }else if (i%3==1) {
            model.status=2;
        }else if (i%3==2) {
            model.status=3;
        }
        [self.dataList addObject:model];
        [self.classDataList addObject:[NSString stringWithFormat:@"音乐学院 170%ld班",(long)i]];
    }
    self.selectClassTableViewHeight=CGRectGetHeight(self.selectClassTableView.frame);
    NSInteger currentHeight = self.classDataList.count * 48;
    if (currentHeight<self.selectClassTableViewHeight) {
        self.selectClassTableViewHeight = currentHeight;
    }
    [self.selectClassTableView reloadData];
    [self.contentTableView reloadData];
    
    [self loadingSubClassNetData];
    
    // 1.注册通知
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(InfoNotificationAction:) name:@"updateStuLeaveListNotification" object:nil];
    
    
}
- (void)InfoNotificationAction:(NSNotification *)notification{
    
    DLog(@"%@",notification.userInfo);
    
    DLog(@"---接收到通知---");
    [self loadingSubClassLeaveAskListNetData];
    
}

-(void) showSelectClassFromeBottomFunction{
    
    [self rotateSelectClass];
    
    SINACustomModelPickView *pickerSingle = [[SINACustomModelPickView alloc]init];
    
    [pickerSingle setDataArray:self.classDataList];
    [pickerSingle setDefalutSelectRowStr:self.currentClassIndex];
    pickerSingle.titleLabel.text=@"选择班级";
    pickerSingle.tag=100;
    
    [pickerSingle setDelegate:self];
    
    [pickerSingle show];
    [self.view endEditing:YES];
}

-(void) showSelectClassFunction{
    __weak __typeof(self) weakSelf = self;
    if (weakSelf.selectContentView.isHidden) {
        weakSelf.selectClassTableView.frame=CGRectMake(0, weakSelf.selectClassTableView.frame.origin.y, weakSelf.selectClassTableView.frame.size.width, 0);
    } else {
        weakSelf.selectClassTableView.frame=CGRectMake(0, weakSelf.selectClassTableView.frame.origin.y, weakSelf.selectClassTableView.frame.size.width,self.selectClassTableViewHeight);
    }
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        if (weakSelf.selectContentView.isHidden) {
            weakSelf.selectFlagImageView.transform=CGAffineTransformMakeRotation(M_PI);
            weakSelf.selectClassTableView.frame=CGRectMake(0, weakSelf.selectClassTableView.frame.origin.y, weakSelf.selectClassTableView.frame.size.width, self.selectClassTableViewHeight);
        } else {
            weakSelf.selectFlagImageView.transform=CGAffineTransformMakeRotation(0);
            weakSelf.selectClassTableView.frame=CGRectMake(0, weakSelf.selectClassTableView.frame.origin.y, weakSelf.selectClassTableView.frame.size.width, 0);
        }
        
        weakSelf.selectContentView.hidden=!weakSelf.selectContentView.hidden;
        
        
    } completion:^(BOOL finished) {
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag==100) {
        return  self.classDataList.count;
    } else {
        return self.dataList.count;
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.tag==100) {
        SINATecManagerSelectClassItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SINATecManagerSelectClassItemTableViewCell"];
        
        if (cell == nil) {
            cell= [[[NSBundle mainBundle]loadNibNamed:@"SINATecManagerSelectClassItemTableViewCell" owner:nil options:nil] firstObject];
            
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell ;
    }else{
        SINATecManagerItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SINATecManagerItemTableViewCell"];
        
        if (cell == nil) {
            cell= [[[NSBundle mainBundle]loadNibNamed:@"SINATecManagerItemTableViewCell" owner:nil options:nil] firstObject];
        }
        SINATecManagerLeaveModel *model =self.dataList[indexPath.row];
        
        cell.itemClickBlock = ^(SINATecManagerLeaveModel *itemModel) {
            SINATecManagerLeaveModel *model =self.dataList[indexPath.row];
            SINATecApprovalManagerDetailViewController *approvalController = [[SINATecApprovalManagerDetailViewController alloc]initWithNibName:@"SINATecApprovalManagerDetailViewController" bundle:nil];
            approvalController.model = model;
            [self.navigationController pushViewController:approvalController animated:YES];
        };
        
        cell.model =model;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell ;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag==100) {
        return  48;
    } else {
        return 146;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak __typeof(self) weakSelf = self;
    if (tableView.tag==100) {
        [weakSelf showSelectClassFunction];
    }else{
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)customModelPickView:(SINACustomModelPickView *)customPickView selectedTitle:(SINACommonPopModel *)selectedModel{
    if (selectedModel.model!=nil) {
        self.currentClassModel = (SINASubClassModel *)selectedModel.model;
    }else{
        self.currentClassModel  = nil;
    }
    
    self.selectClassLabel.text=[NSString stringWithFormat:@"筛选:%@",selectedModel.title];
    [self loadingSubClassLeaveAskListNetData];
}
-(void)dismissCustomPickView:(SINACustomModelPickView *)customPickView{
    [self rotateSelectClass];
}

-(void)dismissBasePickView:(SINABasePickView *)customPickView{
    [self rotateSelectClass];
}
-(void)rotateSelectClass{
    __weak __typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        if ( self.classSelectIsHidden) {
            self.classSelectIsHidden=NO;
            weakSelf.selectFlagImageView.transform=CGAffineTransformMakeRotation(M_PI);
        } else {
            self.classSelectIsHidden=YES;
            weakSelf.selectFlagImageView.transform=CGAffineTransformMakeRotation(0);
        }
    } completion:^(BOOL finished) {
        
    }];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


//查询班级信息
-(void)loadingSubClassNetData{
    
    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    [dict setObject:[[SINACommonUserHelper share]getUserModel].userUid forKey:@"uuid"];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getClassifyByTeacherUuidUrl] withParamters:dict success:^(id responseData) {
        
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            [self.classDataList removeAllObjects];
            
            NSMutableArray *result = responseData[@"content"];
            
            if (result!=nil&&result.count>0) {
                for (NSInteger i=0; i<result.count; i++) {
                    NSDictionary *dict=result[i];
                    SINASubClassModel *model = [[SINASubClassModel alloc]initWithDict:dict];
                    SINACommonPopModel *popModel=[[SINACommonPopModel alloc]init];
                    popModel .title = model.classifyname;
                    popModel.model=model;
                    [self.classDataList addObject:popModel];
                    
                    if (i==0) {
                        self.currentClassModel=model;
                        self.selectClassLabel.text=[NSString stringWithFormat:@"筛选:%@",model.classifyname];
                        [self loadingSubClassLeaveAskListNetData];
                    }
                    
                }
                
                
                
            }else{
                [YPXToastView showToastViewWithText:@"暂无班级数据 " andDuration:3 andParentView:self.selectClassTableView];
            }
            
        }else{
//            NSString *message =responseData[@"message"];
//            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
//                                                                 cancleBtn:nil];
            //[alertView showXLAlertView];
            //self.noDataLabel.hidden=NO;
            //self.mainContentTableView.hidden=YES;
            [YPXToastView showToastViewWithText:@"加载班级数据异常 " andDuration:3 andParentView:self.selectClassTableView];
        }
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        //DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
//        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
//                                                             cancleBtn:nil];
        //[alertView showXLAlertView];
        //        self.noDataLabel.hidden=NO;
        //        self.mainContentTableView.hidden=YES;
        [YPXToastView showToastViewWithText:@"加载班级数据异常 " andDuration:3 andParentView:self.selectClassTableView];
    }];
}


//加载请假信息
-(void)loadingSubClassLeaveAskListNetData{
    
    [self startLoading];
    [self.dataList removeAllObjects];
    [self.contentTableView reloadData];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    //老师uuid String uuid
    [dict setObject:[[SINACommonUserHelper share]getUserModel].userUid forKey:@"uuid"];
    
    //班级id String classifyid
    [dict setObject:self.currentClassModel.classifyid forKey:@"classifyid"];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getApprovalByUuidUrl] withParamters:dict success:^(id responseData) {
        
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            [self.dataList removeAllObjects];
    
            NSMutableArray *result = responseData[@"content"];
            
            if (result!=nil&&result.count>0) {
                for (NSInteger i=0; i<result.count; i++) {
                    NSDictionary *dict=result[i];
                    
                    SINATecManagerLeaveModel  *model = [[SINATecManagerLeaveModel alloc]initWithDict:dict];
                    
                    [self.dataList addObject:model];
                }
                [self.contentTableView reloadData];
            }else{
                [YPXToastView showToastViewWithText:@"暂无班级数据 " andDuration:3 andParentView:self.selectClassTableView];
            }
            
        }else{
//            NSString *message =responseData[@"message"];
//            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
//                                                                 cancleBtn:nil];
            //[alertView showXLAlertView];
            //self.noDataLabel.hidden=NO;
            //self.mainContentTableView.hidden=YES;
            [YPXToastView showToastViewWithText:@"加载班级数据异常 " andDuration:3 andParentView:self.selectClassTableView];
        }

    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
//        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
//                                                             cancleBtn:nil];
        //[alertView showXLAlertView];
        //        self.noDataLabel.hidden=NO;
        //        self.mainContentTableView.hidden=YES;
        [YPXToastView showToastViewWithText:@"加载班级数据异常 " andDuration:3 andParentView:self.selectClassTableView];
    }];
}



@end
